import { getlistApi, addlistApi, updatelistApi, deletelistApi } from '@/api/msbd'
export default {
    namespaced: true,
    state: {
        list: [],
        currentPage: 1,
        pageSize: 5,
        orderby: 'id',
        sort: 'desc',
        total: null,
        searchVal: ""
    },
    mutations: {
        SET_LIST(state, { data, total }) {
            state.list = data
            state.total = total
        },
        SET_PAGESIZE(state, val) {
            state.pageSize = val;
        },
        SET_CURRENTPAGE(state, val) {
            state.currentPage = val
        },
        SET_SEARCHVAL(state, val) {
            state.searchVal = val;
        },
        SET_ORDER_SORT(state, val) {
            if (val.order == "ascending") {
                state.orderby = 'asc'
            } else if (val.order == "descending") {
                state.orderby = "desc"
            } else {
                state.orderby = "id"
            }
            state.sort = val.order
        }
    },
    actions: {
        async getlist({ commit, state }) {
            let r = await getlistApi({
                currentPage: state.currentPage - 1,
                pageSize: state.pageSize,
                orderby: state.orderby,
                sort: state.sort,
                searchVal: state.searchVal
            })
            // console.log(r.data);
            commit('SET_LIST', { data: r.data, total: r.total })
        },
        async addlist({ dispatch }, form) {
            await addlistApi(form);
            dispatch("getlist")
        },
        async updatelist({ dispatch }, form) {
            await updatelistApi(form);
            dispatch("getlist")
        },
        async deletelist({ dispatch }, id) {
            await deletelistApi({ id })
            dispatch("getlist")
        }
    },
    getters: {}
}
